<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Zeta-Daryll
 * Date: 6/17/14
 * Time: 12:09 PM
 * To change this templates use File | Settings | File Templates.
 */
class Admin_category_model extends CI_Model {

    public function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }

    public function insertCateNews($id, $name, $shortDes, $des, $active, $idUser)
    {
        $dbRet = $this->db->query("INSERT INTO category(name, short_description, description, parentid, active, flag, id_user) VALUES('$name', '$shortDes', '$des', '$id', '$active', '0', '$idUser')");
        if(!$dbRet){
            $error = $this->db->_error_message();
            return $error;
        }
        return 0;
    }

    public function getIDNewsCategory($field)
    {
        $query = $this->db->query("SELECT $field FROM category");
        $row = $query->row_array();
        return $row[$field];
    }

    public function getIDNewsCateByName($field, $name)
    {
        $query = $this->db->query("SELECT $field FROM category WHERE name = '$name'");
        $row = $query->row_array();
        return $row[$field];
    }

    public function getTableNews($idUser)
    {
        $dbRet = $this->db->query("SELECT * FROM category WHERE flag = 0");
        if(!$dbRet){
            $error = $this->db->_error_message();
            return $error;
        }
        return $dbRet->result();
    }

    public function getCategories(){
        $this->db->where("active = 1 AND flag = 0"); //cau lenh khong su dung bien trong where
        $this->db->order_by("id");
        $query = $this->db->get("category");
        if(!$query){
            $error = $this->db->_error_message();
            return $error;
        }
        return ($query->result_array());
    }

    public function getSubCategoryNews($idCateNews){
        $this->db->where("id", $idCateNews);
        $this->db->where("flag = 0");
        $this->db->order_by("id");
        $query = $this->db->get("category");
        if(!$query){
            $error = $this->db->_error_message();
            return $error;
        }
        return $query->row_array();
    }

    public function getInfoCateNewsID($id){
        $this->db->where("id", $id);
        $query = $this->db->get("category");
        if(!$query){
            $error = $this->db->_error_message();
            return $error;
        }
        return $query->row_array();
    }

    public function updateCategoryNews($id, $name, $shortDes, $des, $parentid, $active){
        $dbRet = $this->db->query("UPDATE category SET name = '$name', short_description = '$shortDes', description = '$des', parentid = '$parentid', active = '$active'
            WHERE id = '$id'");
        if(!$dbRet){
            $error = $this->db->_error_message();
            return $error;
        }
        return 0;
    }

    public function getCateNewsParentID($field, $id){
        $query = $this->db->query("SELECT $field FROM category WHERE parentid = '$id' AND flag = 0");
        $row = $query->row_array();
        return $row[$field];
    }

    public function deleteCategoryNews($id){
        $dbRet = $this->db->query("UPDATE category SET flag = 1 WHERE id = '$id'");
        if(!$dbRet){
            $error = $this->db->_error_message();
            return $error;
        }
        return 0;
    }

    public function getSubCateNewsIDs($id){
        $dbRet = $this->db->query("SELECT id FROM category WHERE parentid = '$id' AND flag = 0");
        if(!$dbRet){
            $error = $this->db->_error_message();
            return $error;
        }
        return $dbRet->result();
    }

    public function getParentCategory($id){
        $dbRet = $this->db->query("SELECT * FROM category WHERE parentid = '$id' AND flag = 0");
        if(!$dbRet){
            $error = $this->db->_error_message();
            return $error;
        }
        return $dbRet->result();
    }

    public function getNewestCategory(){
        $dbRet = $this->db->query("SELECT * FROM category WHERE parentid = 0 AND flag = 0 ORDER BY id DESC LIMIT 1");
        if(!$dbRet){
            $error = $this->db->_error_message();
            return $error;
        }
        return $dbRet->row_array();
    }

    public function moveThreads($id, $newID){
        $dbRet = $this->db->query("UPDATE product SET id_category = '$newID' WHERE id_category = '$id'");
        if(!$dbRet){
            $error = $this->db->_error_message();
            return $error;
        }
        return 0;
    }

    public function getNameCategoryByID($field, $categoryID){
        $query = $this->db->query("SELECT $field FROM category WHERE id = '$categoryID'");
        $row = $query->row_array();
        return $row[$field];
    }
}
?>